package org.hk.basic.studyspring;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

public class StudyJedis {
    public static void main(String[] args) {

//        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//        jedisPoolConfig.setMaxIdle(8);
//        jedisPoolConfig.setMinIdle(0);
//        jedisPoolConfig.setMaxWaitMillis(2000);
        JedisConnectionFactory jedisConnectionFactory  = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName("127.0.0.1");
        jedisConnectionFactory.setPort(6379);
        jedisConnectionFactory.setPassword(null);
//        jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
        //jedisConnectionFactory.setDatabase(0);
        jedisConnectionFactory.afterPropertiesSet();
        RedisTemplate redisTemplate = new RedisTemplate();

        String key = "zw111";
        String value = "118";
       // RedisCallback redisCallback =
        StringRedisSerializer serializer = new StringRedisSerializer();
        redisTemplate.setDefaultSerializer(serializer);
        redisTemplate.setKeySerializer(serializer);
        redisTemplate.setValueSerializer(serializer);
        redisTemplate.setEnableDefaultSerializer(true);
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        redisTemplate.afterPropertiesSet();//非spring注入一定有先有初始化

        redisTemplate.execute(new RedisCallback<Long>(){
            @Override
            public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
                System.out.println(redisConnection);
                redisConnection.set(key.getBytes(),value.getBytes());
                return 1L;
            }
        });
//        Jedis jedis = new Jedis("127.0.0.1",6379);
//        jedis.set("zw","118");
//        jedis.close();

    }
}


